package keshe.view;

import keshe.ACCOUNT;
import keshe.MyObjectOutputStream;
import keshe.Exception.negativeException;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;

public class InOrOutJFrame extends JFrame implements ActionListener {
    private ACCOUNT account;
    private JTextField jTextField=new JTextField();
    private ArrayList<ACCOUNT> accounts;//用于更新本地文件中的数据
    public InOrOutJFrame(ACCOUNT account, ArrayList<ACCOUNT> accounts){
        this.accounts=accounts;
        this.setAlwaysOnTop(true);
        this.account=account;
        this.setLayout(null);
        JPanel jPanel=new JPanel(new GridLayout(3,1));
        jPanel.setBounds(50,50,200,80);
        this.setBounds(700,300,300,200);
        JLabel jlabel=new JLabel("请输入您要操作的金额：");
        jPanel.add(jlabel);
        jPanel.add(jTextField);
        JLabel jLabel=new JLabel(new ImageIcon("src/keshe/image/inout.jpg"));
        jLabel.setBounds(-200,-1000,500,1200);
        this.getContentPane().add(jPanel);
        this.getContentPane().add(jLabel);
        //控制面板
        GridLayout gridLayout=new GridLayout(1,2);
        JPanel cmdpanel=new JPanel(gridLayout);
        JButton in=new JButton("存入");
        JButton out=new JButton("取出");
        cmdpanel.add(in);
        cmdpanel.add(out);
        jPanel.add(cmdpanel);


        //添加动作事件
        in.addActionListener(e->{
            in();
        });
        out.addActionListener(e->{
            out();
        });

        this.setVisible(true);
    }
    private void in() {
        //存款
        try {
            double money=Double.parseDouble(jTextField.getText());
            for (int i = 0; i < accounts.size(); i++) {
                if(accounts.get(i).equals(account)){
                    accounts.get(i).depositMoney(money);
                }
            }
            Show("存款成功!   您的余额为："+account.getMoney());
        } catch (NumberFormatException e) {
            Show("存入的金额不符合规则");
        } catch (negativeException e) {
            Show(e.getMessage());
        }
        this.dispose();
    }
    private void out(){
        //取款
        try {
            double money=Double.parseDouble(jTextField.getText());
            for (int i = 0; i < accounts.size(); i++) {
                if(accounts.get(i).equals(account)){
                    accounts.get(i).drawMoney(money);
                }
            }
            Show("取款成功!  您的余额为："+account.getMoney());
        } catch (NumberFormatException e) {
            Show("取出的金额不符合规则");
        } catch (negativeException e) {
            Show(e.getMessage()+"您的余额为："+account.getMoney());
        }
        this.dispose();
    }
    public ArrayList<ACCOUNT> getAccounts() {
        return accounts;
    }
    private void CINData(ArrayList<ACCOUNT> accounts) {

        MyObjectOutputStream oos= null;
        try {
            oos = new MyObjectOutputStream(new FileOutputStream("AccountData/Accounts",false));
            for (ACCOUNT account1:accounts) {
                oos.writeObject(account1);
            }
            oos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void Show(String message){
        JOptionPane.showMessageDialog(this,message);
    }

    public static void main(String[] args) {
        new InOrOutJFrame(new ACCOUNT(),new ArrayList<>());
    }
    @Override
    public void actionPerformed(ActionEvent e) {

    }
}
